package com.ruoyi.mateThrough.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateProcurement.service.MateProcurementService;
import com.ruoyi.mateRequistion.domain.MateRequistion;
import com.ruoyi.mateRequistion.service.MateRequistionService;
import com.ruoyi.mateThrough.domain.MateThrough;
import com.ruoyi.mateThrough.service.MateThroughService;
import com.ruoyi.material.domain.MaterialManufactuter;
import com.ruoyi.material.domain.MaterialProduct;
import com.ruoyi.material.service.MateManufactuterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mateThrough")
public class MateThroughController extends BaseController {

    @Autowired
    MateManufactuterService mateManufactuterService;
    @Autowired
    MateProcurementService mateProcurementService;
    @Autowired
    MateThroughService mateThroughService;


    @Autowired
    MateRequistionService mateRequistionService;

    @GetMapping("/AuditRequistion")
    public Map<String, Object> AuditRequistion() {
        Map<String, Object> resMap = new HashMap<>();
        List<MateRequistion> list = mateRequistionService.AuditRequistion(SecurityUtils.getDeptId());
        resMap.put("result", list);
        return resMap;
    }

    @GetMapping("/selectProductModelLists")
    @ResponseBody
    public TableDataInfo selectProductModelLists(@RequestParam(value = "materialId", required = false) Long materialId,
                                                 @RequestParam(value = "mateProductName", required = false) String mateProductName,
                                                 @RequestParam(value = "mateProductType", required = false) String mateProductType) {
        startPage();
        Long deptId = SecurityUtils.getLoginUser().getUser().getCompanyId();
        MaterialProduct product = new MaterialProduct();
        product.setMaterialId(materialId);
        product.setCompanyId(deptId);
        product.setMateProductName(mateProductName);
        product.setMateProductType(mateProductType);
        return getDataTable(mateProcurementService.selectProductLists(product));
    }

    @PostMapping("/addThrough")
    @ResponseBody
    public AjaxResult addThrough(@RequestBody MateThrough mateThrough) {
        mateThrough.setDeptId(SecurityUtils.getDeptId());
        int a = mateThroughService.addThrough(mateThrough);
        if (a >= 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 删除放行单
     */

    @Log(title = "删除生产计划", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{throughId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("throughId") Long throughId) {
        mateThroughService.remove(throughId);
        return AjaxResult.success("删除成功");
    }

    //查询当前公司下所有的放行单
    @GetMapping("/selectThrough")
    @ResponseBody
    public TableDataInfo selectThrough(@RequestParam(value = "beginTime", required = false) String beginTime,
                                       @RequestParam(value = "endTime", required = false) String endTime) {

        MateThrough mateThrough = new MateThrough();
        mateThrough.setBeginTime(beginTime);
        mateThrough.setEndTime(endTime);
        mateThrough.setDeptId(SecurityUtils.getDeptId());
        startPage();
        List<MateThrough> list = mateThroughService.selectThrough(mateThrough);
        return getDataTable(list);
    }

    @GetMapping("/selectEdit")
    @ResponseBody
    public Map<String, Object> selectEdit(@RequestParam(value = "throughId", required = false) Long throughId) {
        Map<String, Object> resMap = new HashMap<>();
        MateThrough mateThrough = mateThroughService.selectMateThrough(throughId);
        resMap.put("result", mateThrough);
        return resMap;
    }

    @PostMapping("/editThrough")
    @ResponseBody
    public AjaxResult editThrough(@RequestBody MateThrough mateThrough) {
        int a = mateThroughService.updatemateThrough(mateThrough);
        if (a > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }

    }

}
